package com.fsnbigdata.repository;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.Distribution;
import com.fsnbigdata.domain.Organism;
import com.fsnbigdata.domain.QDistribution;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * = DistributionRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = DistributionRepositoryCustom.class)
@Transactional(readOnly = true)
public class DistributionRepositoryImpl extends QueryDslRepositorySupportExt<Distribution> implements DistributionRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    DistributionRepositoryImpl() {
        super(Distribution.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String COUNTRY = "country";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CITY = "city";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String PROVINCE = "province";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String DETAIL = "detail";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ORGANISM = "organism";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String STANDARD = "standard";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String COUNTY = "county";

    /**
     * TODO Auto-generated method documentation
     *
     * @param organism
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<Distribution> findByOrganism(Organism organism, GlobalSearch globalSearch, Pageable pageable) {
        QDistribution distribution = QDistribution.distribution;
        JPQLQuery<Distribution> query = from(distribution);
        Assert.notNull(organism, "organism is required");
        query.where(distribution.organism.eq(organism));
        Path<?>[] paths = new Path<?>[] { distribution.organism, distribution.standard, distribution.country, distribution.province, distribution.city, distribution.county, distribution.detail };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(ORGANISM, distribution.organism).map(STANDARD, distribution.standard).map(COUNTRY, distribution.country).map(PROVINCE, distribution.province).map(CITY, distribution.city).map(COUNTY, distribution.county).map(DETAIL, distribution.detail);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, distribution);
    }
}
